package com.jmxcfc.blfsc.langyu.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * Created by xyyou
 * 2023/10/23 9:26
 */
@Slf4j
public class MD5Util {

    /**
     * md5加密一（Java自身包实现-32位小写加密）
     * @param str
     * @return
     */
    public static String md5(String str) {

        byte[] secretBytes ;
        try {
            secretBytes = MessageDigest.getInstance("MD5").digest(
                    str.getBytes("UTF-8"));
        } catch (Exception e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        StringBuffer md5code = new StringBuffer();
        for(int i = 0; i < secretBytes.length; ++i) {
            md5code = md5code.append(byteToHexString(secretBytes[i]));
        }

        return md5code.toString();
    }

    private static String byteToHexString(byte b) {
        String[] HexCode = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
        int n = b;
        if (b < 0) {
            n = b + 256;
        }

        int d1 = n / 16;
        int d2 = n % 16;
        return HexCode[d1] + HexCode[d2];
    }

    public static void main(String[] args) {
        //f6650849eb42c04d9b7652e85d3014a2
        //String test = "jm0828w7f7sl8298-5374258-dcbf85-c87e5-9c1ae6-bfg69";
        String test = "jmx_hyjmx_hy12320241105104521";
        String encrptString = MD5Util.md5(test);
        log.info("encrptString:{}",encrptString);
        log.info("current:{}", System.currentTimeMillis());
    }
}
